package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sl.au.entity.Account;
import com.sl.au.entity.ConfigDetailForCRM;
import com.sl.au.entity.ConfigMainForCRM;
import com.sl.au.entity.MFOldPartRelation;
import com.sl.au.entity.ProductPlanForCRM;
import com.sl.au.entity.Quote;
import com.sl.au.entity.QuoteReplacement;
import com.sl.au.service.ConfigDetailForCRMService;
import com.sl.au.service.QuoteService;

@RestController
@RequestMapping(value = "/ui/configdetailfor/")
public class ConfigDetailForCRMController extends BaseController {
	
	@Autowired
	ConfigDetailForCRMService configDetailForCRMService;
	
	@Autowired
	QuoteService quoteService;
	
	@RequestMapping(value = "getpagehisquote", method = RequestMethod.GET)
	public Map<String, Object> getpagehisquote(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		Account a = this.getMyAccount();
		//String name="contractsearch"; 
        //Module module = this.getPageModule(name);
        String userId = a.getUser().getId();
		//String superior = a.getUser().getSaleArea().getSuperior(); 
		//String areaName=a.getUser().getSaleArea().getAreaName();
        //String dataFilter=module.getDataFilter();
        String key = request.getParameter("key");
        if (key == null) {
			key = "";
		}
		key = key.replaceAll("\\s+", "");
		condition.put("key", key);
		List<Quote> contracts = quoteService.getpagehisquote(start, length, condition, null, null);
		long count = quoteService.getpagehisquoteCount(condition);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "getpagehisdetail", method = RequestMethod.GET)
	public Map<String, Object> getpagehisdetail(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		Account a = this.getMyAccount();
		//String name="contractsearch"; 
        //Module module = this.getPageModule(name);
        String userId = a.getUser().getId();
		//String superior = a.getUser().getSaleArea().getSuperior(); 
		//String areaName=a.getUser().getSaleArea().getAreaName();
        //String dataFilter=module.getDataFilter();
        String key = request.getParameter("key");
        String quoteid = request.getParameter("quoteid");
        if (key == null) {
			key = "";
		}
		key = key.replaceAll("\\s+", "");
		condition.put("key", key);
		condition.put("quoteid", quoteid);
		List<QuoteReplacement> contracts = quoteService.getHisDetails(start, length, condition, null, null);
		long count = quoteService.getHisDetailsCount(condition);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "getpagemf", method = RequestMethod.GET)
	public Map<String, Object> getpagemf(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		Account a = this.getMyAccount();
		//String name="contractsearch"; 
        //Module module = this.getPageModule(name);
        String userId = a.getUser().getId();
		//String superior = a.getUser().getSaleArea().getSuperior(); 
		//String areaName=a.getUser().getSaleArea().getAreaName();
        //String dataFilter=module.getDataFilter();
        String key = request.getParameter("key");
        if (key == null) {
			key = "";
		}
		key = key.replaceAll("\\s+", "");
		condition.put("key", key);
		List<MFOldPartRelation> contracts = configDetailForCRMService.getMFDetails(start, length, condition, null, null);
		long count = configDetailForCRMService.getMFDetailsCount(condition);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "getpagemfdown", method = RequestMethod.GET)
	public Map<String, Object> getpagemfdown(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String opid = request.getParameter("opid");
		String ntype = request.getParameter("ntype");
		int id = -1;
		int type = 2;
		if(ntype.equals("其他")){
			id = Integer.parseInt(opid);
			type = 3;
		}else{
			id = Integer.parseInt(request.getParameter("ntype"));
		}
		String key = request.getParameter("key");
		if(key == null){
			key = "";
		}
		List<MFOldPartRelation> contracts = configDetailForCRMService.getpagemfsecond(type, key, id);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", contracts.size());
		map.put("recordsTotal", contracts.size());
		return map;
	}
	
	
	@RequestMapping(value = "getpagemfsecond", method = RequestMethod.POST)
	public Map<String, Object> getpagemfsecond(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int opid = Integer.parseInt(request.getParameter("opid"));
		List<MFOldPartRelation> contracts = configDetailForCRMService.getpagemfsecond(1, "", opid);
		map.put("data", contracts);
		return map;
	}
	
	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		Account a = this.getMyAccount();
		//String name="contractsearch"; 
        //Module module = this.getPageModule(name);
        String userId = a.getUser().getId();
		//String superior = a.getUser().getSaleArea().getSuperior(); 
		//String areaName=a.getUser().getSaleArea().getAreaName();
        //String dataFilter=module.getDataFilter();
        String key = request.getParameter("key");
        String opId = request.getParameter("opid");
        String ntype = request.getParameter("ntype");
        if (key == null) {
			key = "";
		}
        if (ntype == null) {
        	ntype = "";
		}
		key = key.replaceAll("\\s+", "");
		ntype = ntype.replaceAll("\\s+", "");
		condition.put("key", key);
		condition.put("opId", opId);
		condition.put("ntype", ntype);
        List<ConfigDetailForCRM> contracts = configDetailForCRMService.getDetails(start, length, condition, null, null);
		long count = configDetailForCRMService.getDetailsCount(condition);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "getproductplanpage", method = RequestMethod.GET)
	public Map<String, Object> getproductplanpage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		Account a = this.getMyAccount();
		//String name="contractsearch"; 
        //Module module = this.getPageModule(name);
        String userId = a.getUser().getId();
		//String superior = a.getUser().getSaleArea().getSuperior(); 
		//String areaName=a.getUser().getSaleArea().getAreaName();
        //String dataFilter=module.getDataFilter();
        String key = request.getParameter("key");
        if (key == null) {
			key = "";
		}
		key = key.replaceAll("\\s+", "");
		
		condition.put("key", key);
		List<ProductPlanForCRM> contracts = configDetailForCRMService.getContractDetails(start, length, condition, null, null);
		long count = configDetailForCRMService.getDetailsContractCount(condition);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "saveOtherEROld", method = RequestMethod.GET)
	public Map<String, Object> saveOtherEROld(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String quoteId = request.getParameter("quoteId");
		String ids = request.getParameter("ids");
		String id[] = ids.split(",");
		int code = configDetailForCRMService.saveOtherEROld(id, quoteId);
		quoteService.reCheckQuoteIndex(quoteId);
		map.put("code", code);
		return map;
	}
	
	@RequestMapping(value = "getconfigmainlist", method = RequestMethod.POST)
	public Map<String, Object> getconfigmainlist(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String opid = request.getParameter("opid");
		List<ConfigMainForCRM> list = configDetailForCRMService.getconfigmainlist(opid);
		map.put("data", list);
		map.put("code", "0");
		return map;
	}
	
	@RequestMapping(value = "saveOtherERMF", method = RequestMethod.GET)
	public Map<String, Object> saveOtherERMF(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String quoteId = request.getParameter("quoteId");
		String ids = request.getParameter("ids");
		String id[] = ids.split(",");
		int code = configDetailForCRMService.saveOtherERMF(id, quoteId);
		quoteService.reCheckQuoteIndex(quoteId);
		map.put("code", code);
		return map;
	}
	
	@RequestMapping(value = "saveOtherER", method = RequestMethod.GET)
	public Map<String, Object> saveOtherER(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String quoteId = request.getParameter("quoteId");
		String ids = request.getParameter("ids");
		String strindex = request.getParameter("indexOrder");
		int indexOrder = 0;
		if(!strindex.equals("undefined")&&!strindex.equals("")){
			indexOrder = Integer.parseInt(strindex);
		}
		String id[] = ids.split(",");
		int code = configDetailForCRMService.saveOtherER(id, quoteId,indexOrder);
		quoteService.reCheckQuoteIndex(quoteId);
		map.put("code", code);
		return map;
	}
	@RequestMapping(value = "saveOtherERquoteFor", method = RequestMethod.GET)
	public Map<String, Object> saveOtherERquoteFor(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String quoteForeignId = request.getParameter("quoteForeignId");
		String ids = request.getParameter("ids");
		String id[] = ids.split(",");
		int code = configDetailForCRMService.saveOtherER(id, quoteForeignId, 0);
		map.put("code", code);
		return map;
	}

}
